<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>百日新高</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">						
			p {margin:1px}
			ul {margin:1px}			
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			table{
			width:380px; 
			border-collapse:collapse; 
			text-align:center;
			}
			td,th{border:1px solid #696969}	
		</style>
	</head>
	<body>
		<div id="towrite" class="fixedBox" style="display:none;"></div>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
		<script>
			var bkcolor = $("body").css("backgroundColor");
			var dt = "";
			var tt = "";
			var gt = "";
			var k = "0";
			window.onload = function() {
				var day = new Date();
				gt = day.getTime();
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;	
				dt = tt;
				start();
				changeStyle();
			}
			
			function changeStyle() {
				var obj = document.getElementById("towrite");
				obj.style.display= "block";
				obj.style.left= "0px"; 				
				obj.style.width= "600px";	
				//obj1.style.height= "620px";		
				obj.style.fontSize="12px";				 
			}
			
			
			Date.prototype.Format = function (fmt) {
			var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
			};
			
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			function NumberTransform(num) { 
				var result = num;
               if(Math.abs(num) > 100000000){
			    result = num / 100000000;
				result = result.toFixed(2) + "亿";
			   }else if(Math.abs(num) > 10000){
			    result = num/10000;
				result = result.toFixed(0) + "万";
			   }				   
               return result;  
			}
			
			var trid = "";
			function changeCss(obj,hid,id){					
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					var idz = tlist[i].id.split("#")[0];
					var dm = tlist[i].id.split("#")[1];
					if(idz.indexOf("新增") > -1 || dms.indexOf(dm) == -1){						
						tlist[i].style.backgroundColor = bkcolor;
					}else{
						tlist[i].style.backgroundColor = gcolor;
					}					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			var gcolor = "#8B008B";
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头							
							var idz = this.id.split("#")[0];
							var dm = this.id.split("#")[1];	
							if(idz.indexOf("新增") > -1){
								this.style.background = bkcolor;
								if(this.id == trid){									
									this.style.background="#483D8B";
								}
							}else{								
								if(this.id != trid && dms.indexOf(dm) == -1){  //过滤选中行
									this.style.background = bkcolor;
								}else{
									if(dms.indexOf(dm) > -1 && this.id != trid){
										this.style.background= gcolor;
									}else{
										this.style.background="#483D8B";
									}
								}
							}						
						}
					}					
				}
			}
			
			function decodeUnicode(str) { 
				str = str.replace(/\\/g, "%"); 
				return unescape(str);
			}
						
			function cDayFunc(dp){ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			function BookingRmk(date){         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);
				//alert("tt:"+tt+"|"+"dt:"+dt);				
				k = 0;
				if(date1.getTime() < date2.getTime()){			
					k = 1;
					tradeday();
				}				
				start();						
			}
			
			function prev(){  //前一天
				pn = -1;
				var day = new Date(tt);	
				day.setTime(day.getTime() - 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);
			}
			
			function next(){  //后一天 				
				pn = 1;
				var day = new Date(tt);	
				day.setTime(day.getTime() + 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);							
			}
			
			var pn = -1;
			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() + pn*24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");
					//alert(tt);
					if(tt == dt){	
						k = 0;
						start();
						break;
					}
					var url = "https://apphis.longhuvip.com/w1/api/index.php?a=GroupStock&st=1000&apiv=w26&Type=0_0_0_0_0&c=StockNewHigh&Date=" + tt;									
					var res = [];
					$.ajax({
						type: "get",
						url: url,
						dataType: "json",
						async : false,
						cache:false,
						timeout:3000,						
						success: function(data) {							
							res = data;
						}
					});	
					if(res.GroupList.length > 0){
						break;
					}	
				}
			}				
			
			function start(){  //先列表
				div = document.getElementById('towrite');
				div.innerHTML = '<font size="3">百日新高&nbsp;&nbsp;</font><input style="width: 100px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" /><font id="bt" size="2" style="float:right;margin-right:100px;"></font>';										
				var back = '<a href="javascript:prev();"  style="padding-left:10px;font-size:16px;color:#fff;">←</a>';				
				var forward = '<a href="javascript:next();"  style="font-size:16px;color:#fff;">→</a>';
				div.innerHTML += back + forward ;
				$("#bt").text('载入数据……');
				var url1 = "https://apphq.longhuvip.com/w1/api/index.php?a=GroupStock&st=100&apiv=w26&Type=0_0_0_0_0&c=StockNewHigh&"; 								
				var url2 = "https://apphis.longhuvip.com/w1/api/index.php?a=GroupStock&st=1000&apiv=w26&Type=0_0_0_0_0&c=StockNewHigh&Date=" + tt + "&";				
				var url = url1;
				if(k == "1" ){
				  url = url2;
				}
				var res = [];
				$.ajax({
					type: "get",
					url: url,
					dataType: "json",
					async : false,
					cache:false,
					timeout:3000,					
					success: function(data) {
						//alert(JSON.stringify(data))						
						//document.write(JSON.stringify(data));							
						if(k == 0){
							tt = data.Date;							
							dt = tt;
						}
						
						x = data.GroupList.length;							
						for (i = 0; i < data.GroupList.length; i++){
							res = [];
							var aa = data.GroupList[i];
							var id = aa.GroupName;
							var name = "<a style='color:#fff;font-size:14px' href='javascript:void(0)' id='id" + id + "' onclick='update(\"" + id + "\")'>" + id + "（" + aa.List.length + "<span id='jr" + id + "'></span>" + "）</a>";
							div.innerHTML += "<p style='margin-left:5px;'>" + name + "</p>";						
							
							var newDiv = document.createElement("div");
							newDiv.id = id;
							<!-- if(i == 0){ -->
								<!-- newDiv.style.display = "block"; -->
								<!-- temp = id + ";"; -->
							<!-- }else{ -->
								<!-- newDiv.style.display = "none"; -->
							<!-- } -->
							newDiv.style.display = "none";
							div.appendChild(newDiv);							
						}											
					}
				});	
				document.getElementById("PriceDate").value = tt;
				setTimeout(function(){getSource()}, 100); 
			}
			
			var dmz = "";
			var dms = "";
			function getSource() {		//再注入div		
				var url1 = "https://apphq.longhuvip.com/w1/api/index.php?a=GroupStock&st=100&apiv=w26&Type=0_0_0_0_0&c=StockNewHigh&"; 								
				var url2 = "https://apphis.longhuvip.com/w1/api/index.php?a=GroupStock&st=1000&apiv=w26&Type=0_0_0_0_0&c=StockNewHigh&Date=" + tt + "&";				
				var url = url1;
				if(k == "1" ){
				  url = url2;
				}					
											
				var x = 0;
				var res = [];
				$.ajax({
					type: "get",
					url: url,
					dataType: "json",
					async : false,
					cache:false,
					timeout:3000,					
					success: function(data) {						
						x = data.GroupList.length;													
						for (var i = 0; i < data.GroupList.length; i++){
							res = [];
							var aa = data.GroupList[i];
							var id = aa.GroupName;
							var n = 0;
							dm = "";
							for (var j = 0; j < aa.List.length; j++){
								var code = aa.List[j][0];
								var news = aa.List[j][9];  //新增
								if(id.indexOf("新增") > -1){  //全部新增
									dms += code + ";";
								}else if(dms != ""){
									//if(dms.indexOf(code) > -1){
									if(news == 1){
										n++;
										dmz += code + ";";  //板块新增
									}
								}
								res.push({"code":aa.List[j][0],"name":aa.List[j][1],"xj":aa.List[j][2],"zf":aa.List[j][3],
									"sjhs":aa.List[j][6],"sjlt":NumberTransform(aa.List[j][7]),"bk":aa.List[j][4],"zt":aa.List[j][10]
								});	
							}
							//alert(n)
							if(n > 0){
								$("#jr" + id).text("，今日新增：" + n);
							}
							tables(res,id);							
						}											
					}
				});		
									
				$("#bt").text('');	
											
				//$("div a:first").click();
				
				for(var i = 0; i < x; i++){
					var table = document.getElementsByTagName("table")[i];
					if(i > 0){
						changeTable(table,"7")
					}
					makeSortable(table);
					hover();
				}						
			}
			
			var temp = "";	
			function update(ss) {				
				if(document.getElementById(ss)){					
					div1 = document.getElementById(ss);
					if(temp.indexOf(ss) == -1){
						temp += ss + ";";
						div1.style.display = "block";
					}else{
						temp = temp.replace(ss + ";" , "");
						div1.style.display = "none";
					}
				}											
			}
			var bj = [];
			function tables(data,id) {
				//if(data != ""){									
					//var json = JSON.parse(data);
				//alert(JSON.stringify(data))		
				if(data.length > 0){	
					var json = data;
					
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var n = 0;
					var b = 0;
					var gl = "涨幅";  //高亮
					for(var key in json[0]){  //表头
						key = key.replace('code','代码').replace('name','名称').replace('zf','涨幅%').replace('xj','现价').replace('sjlt','实际流通');
						key = key.replace('sjhs','实际换手').replace('zt','主题').replace('bk','板块');
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);
						if(gl == key.replace("%","")){
							n = m;
							th.style.color = "#FF6633";	
						}
						if(key == "板块" || key == "主题"){
							bj.push(m)
							th.style.color = "#FFA500";	
						}
						m++;
					}							
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						var mc = "";
						for(var key in json[i]){ //行元素遍历						
							var td=document.createElement("td");							
							
							var rc = json[i][key];
							if(p == 1){  //代码列
								dm = rc;
								tr.id = id + "#" + dm;
							}else if(p == 5){
								rc = keepTwoDecimal(rc);
							}					
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + tr.id + "\",\"towrite\")'>" + rc + "</a>";
							
							if(dmz.indexOf(dm) > -1){
								tr.style.backgroundColor  = gcolor;
							}
							if(p == n + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FF6633";
							}
							if(bj.indexOf(p - 1) > -1){						
								td.getElementsByTagName("a")[0].style.color = "#FFA500";
							}																
							tr.appendChild(td);							
							p++;
						}													
						tbody.appendChild(tr);																
					}
					
					<!-- var div = document.getElementById("towrite"); -->
					<!-- var newDiv = document.createElement("div"); -->
					<!-- newDiv.id = id; -->
					<!-- newDiv.style.display = "none"; -->
					<!-- div.appendChild(newDiv); -->
					var div1 = document.getElementById(id);												
					div1.appendChild(table);
					table.id = "table" + id;										
				}						
			}
			
			//隐藏某列				
            function changeTable(table,ss) {	
				var trs = table.rows;
				ss = ss + '#';
				var aa = ss.split('#');
				for (var j = 0; j < aa.length; j++) {
					if(aa[j] != ""){
						for (var i = 0, len = trs.length; i < len; i++) {					
							var p = aa[j];
							var cell = trs[i].cells[p]; 
							cell.style.display = 'none';
						}
					}
				}					
			}
			
			function makeSortable(table) {			
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){						
							showCol(n,bj);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("主力") != -1 || headtxt.indexOf("连板") != -1 || headtxt.indexOf("板块") != -1){
									val1 = val1;
									val2 = val2;
								}else{
									if(val1 == "-"){
										val1 = " ";
									}
									if(val2 == "-"){
										val2 = " ";
									}
									val1 = val1.replace(" ","0").replace("无","1000").replace("NaN","0");
									val2 = val2.replace(" ","0").replace("无","1000").replace("NaN","0");
									if(val1.indexOf("万") != -1){
										val1 = parseFloat(val1)*10000;
									}else if(val1.indexOf("亿") != -1){
										val1 = parseFloat(val1)*100000000;
									}else{
										val1 = parseFloat(val1);
									}
									
									if(val2.indexOf("万") != -1){
										val2 = parseFloat(val2)*10000;
									}else if(val2.indexOf("亿") != -1){
										val2 = parseFloat(val2)*100000000;
									}else{
										val2 = parseFloat(val2);
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(iCol,arr){	 //(选中列，标记列)	
				var oTable = document.getElementsByTagName('table')[0];						
				//表头
				var th = oTable.getElementsByTagName("th");						
				for (var i=0;i < th.length; i++){	    //列遍历
					th[i].style.color = "#E5E5E5";      //还原					
					if(arr.length > 0 && arr.indexOf(i) > -1){  //BJ列高亮
						th[i].style.color = "#FFA500";
					}
					th[iCol].style.color = "#FFFF80";	//选中				
				}
				
				var tr = oTable.getElementsByTagName("tr");	
				for (var i=1;i < tr.length; i++){	//行遍历
					var otagsA = tr[i].getElementsByTagName("a");
					for (var j=0;j < otagsA.length; j++){	//列遍历
						otagsA[j].style.color = "#E5E5E5";	//还原																
						if(arr.length > 0 && arr.indexOf(j) > -1){  //BJ列高亮
							otagsA[j].style.color = "#FFA500";
						}
					}
					var value = otagsA[iCol].innerText.replace(/(万)|(亿)/g,'');
					if(!isNaN(parseFloat(value)) && isFinite(value)){  //判断列是否为数字
						if(parseFloat(value) >= 0){
							otagsA[iCol].style.color = "#FF6633";     //正负颜色
						}else{
							otagsA[iCol].style.color = "#00FF00";
						}
					}else{
						otagsA[iCol].style.color = "#FF6633";
					}					
				}					
			}	
			
			
		</script>	
	</body>
</html>